/*
 * @author Robert Hanwell
 * @version 3
 * @since January
 */
package com.smartbox.floodaction;

import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

// TODO: Auto-generated Javadoc
/**
 * The Class CreateEditTask.
 */
public class CreateEditTask extends BaseActivity implements
		OnItemSelectedListener {

	/** The tsk notes. */
	private EditText tskTitle, tskNotes;

	/** The row pk. */
	private Long grpKey, rowPK;

	/** The map button. */
	private Button mapButton;

	/** The get grp fk. */
	private Bundle getGrpFK;

	/** The d. */
	private Dialog d;

	/** The db helper. */
	private TaskData dbHelper;

	/** The m cur. */
	private Cursor mCur;

	/** The update flag. */
	private boolean updateFlag = false;

	/** The photo ref. */
	private String stTaskTitle, stTaskNote, mapLat, mapLong, photoRef;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_edit_task);
		dbHelper = new TaskData(this);
		dbHelper.open();
		d = new Dialog(this);
		initialize();
		getGrpFK = getIntent().getExtras();
		if (getGrpFK.getLong("taskId") > 0) {
			rowPK = getGrpFK.getLong("taskId");
			updateFlag = true;
			valuesToEdit();
		}
		grpKey = getGrpFK.getLong("GRPPK");
		Toast.makeText(this, grpKey.toString(), Toast.LENGTH_LONG).show();
		mapButton.setOnClickListener(new View.OnClickListener() {

			/*
			 * (non-Javadoc)
			 * 
			 * @see android.view.View.OnClickListener#onClick(android.view.View)
			 */
			@Override
			public void onClick(View v) {

				Intent map = new Intent("com.smartbox.floodaction.TASKMAP");
				startActivity(map);

			}
		});
	}

	/**
	 * Values to edit.
	 */
	private void valuesToEdit() {

		mCur = dbHelper.getTaskByAction(rowPK);
		startManagingCursor(mCur);
		if (mCur.moveToFirst()) {
			tskTitle.setText(mCur.getString(mCur
					.getColumnIndexOrThrow(SQLHelper.KEY_TSK_NAME)));
			tskNotes.setText(mCur.getString(mCur
					.getColumnIndexOrThrow(SQLHelper.KEY_TSK_NOTES)));
		}

	}

	/**
	 * Initialize.
	 */
	public void initialize() {
		tskTitle = (EditText) findViewById(R.id.etTaskTitle);
		tskNotes = (EditText) findViewById(R.id.etTaskNotes);
		mapButton = (Button) findViewById(R.id.bMap);

	}

	// pop menu out
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.smartbox.floodaction.BaseActivity#onCreateOptionsMenu(android.view
	 * .Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater menuInf = getMenuInflater();

		if (updateFlag) {
			menuInf.inflate(R.menu.update_menu, menu);

		} else {
			menuInf.inflate(R.menu.submit_menu, menu);
		}

		return true;
	}

	/**
	 * Validate.
	 * 
	 * @return true, if successful
	 */
	public boolean validate() {
		stTaskTitle = tskTitle.getText().toString();
		stTaskNote = tskNotes.getText().toString();
		mapLat = null;
		mapLong = null;
		photoRef = null;
		if (stTaskTitle.equalsIgnoreCase("")) {
			Toast.makeText(this, "Sorry You need a Title", Toast.LENGTH_LONG)
					.show();
			return false;
		}
		if (stTaskNote.equalsIgnoreCase("")) {
			Toast.makeText(this, "Sorry you need a Note", Toast.LENGTH_LONG)
					.show();
		}
		return true;
	}

	// hardkey menu options
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.smartbox.floodaction.BaseActivity#onOptionsItemSelected(android.view
	 * .MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.bUpdate:
			if (validate()) {
				try {
					dbHelper.updateEntryTask(rowPK, stTaskTitle, stTaskNote,
							mapLat, mapLong, photoRef);
					Toast.makeText(this, "Updated", Toast.LENGTH_SHORT).show();
				} catch (Exception e) {
					String error = e.toString();

					TextView tv = new TextView(this);
					tv.setText(error);
					d.setContentView(tv);
					d.show();
				} finally {
					Intent i = new Intent(
							"com.smartbox.floodaction.SINGLETASKVIEW");
					i.putExtra("taskId", rowPK);
					startActivity(i);
					finish();
				}
			}
			return true;
		case R.id.bSubmit:
			// submit data
			if (validate()) {
				try {

					rowPK = dbHelper.createEntryTask(grpKey, stTaskTitle,
							stTaskNote, mapLat, mapLong, photoRef);

					Intent viewPlan = new Intent(
							"com.smartbox.floodaction.CAPTABBAR");
					startActivity(viewPlan);
					Toast.makeText(this, "Submitted" + rowPK.toString(),
							Toast.LENGTH_SHORT).show();

					finish();
				} catch (Exception e) {
					String error = e.toString();
					TextView tv = new TextView(this);
					tv.setText(error);
					d.setContentView(tv);
					d.show();
				} finally {

				}
			}
			return true;
		case R.id.bReset:

			return true;

		}

		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android
	 * .widget.AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.AdapterView.OnItemSelectedListener#onNothingSelected(android
	 * .widget.AdapterView)
	 */
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {

	}

	// after create start database connection
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		super.onStart();
		Log.i("onStart", "open database");
		dbHelper.open();

	}

	// before we close the screen close the database
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		super.onStop();
		Log.i("onStop", "closing database");
		dbHelper.close();

	}

}
